<?php
if (!defined('CMS_SYSTEM')) die();
if ($disable_site) {
	echo "<center style=\"margin-top:200px;font-weight:bold\">".$disable_message ."</center>";
}else{
$catid = intval($_GET['catid']);

$result = $db->sql_query("SELECT catid, parent FROM {$prefix}_news_cat WHERE alanguage='$currentlang' ORDER BY weight, catid ASC");
if ($db->sql_numrows($result) > 0) {
	$i = 0;
	$tempArr = array();
	while ($rows = $db->sql_fetchrow($result)) {
		list($tempArr[$i]['id'], $tempArr[$i]['parent']) = $rows;
		$i++;
	}
}
$newArr = array();
Common::buildTree($tempArr, $newArr);
$searchArray = Common::recursiveArrayKeyExists($catid, $newArr);
if ($searchArray === false) header("Location: index.php");
$kList = '';
if (is_array($searchArray[$catid])) Common::findAllKeys($searchArray[$catid], $kList);
else $kList = strval($catid);
if (substr($kList, -1) == ':') $kList = substr($kList, 0, strlen($kList) - 1);
$kList = explode(':', $kList);

$result = $db->sql_query("SELECT title, startid FROM ".$prefix."_news_cat WHERE alanguage='$currentlang' AND catid=$catid");
if($db->sql_numrows($result) != 1) header("Location: index.php");

list($catname, $startid) = $db->sql_fetchrow($result);

$page_title .= " :: $catname";
include_once("templates/".$Default_Temp."/header.php");
  include_once("templates/".$Default_Temp."/banner.php");
  include_once("templates/".$Default_Temp."/menu.php");
  	 echo "<div id=\"wrapper\">";
	 echo "<div class=\"content fix\">";
	$result_cate = $db->sql_query("SELECT catid, parent FROM {$prefix}_news_cat WHERE parent='$catid' AND alanguage='$currentlang' ORDER BY weight, catid ASC"); 
	$item_list_tmp = '';
	if($db->sql_numrows($result_cate) > 0) {
	 while(list($catid_pr, $parent) = $db->sql_fetchrow($result_cate)) 
      {
		if ($catid_pr !== '')
			{
		      $item_list_tmp .= $item_list_tmp ? ",'$catid_pr'" : "'$catid_pr'";
			}
	    }
       $item_list_tmp .= $item_list_tmp ? ",'$catid'" : "'$catid'";
	}
	if(empty($item_list_tmp))
	 {
	       /*
	     * Phan lay ra danh sach tin tuc
	     * Le duc vien
	     */
	     $perpage = $perpage;
		 $page = intval(isset($_GET['page']) ? $_GET['page'] : (isset($_POST['page']) ? $_POST['page']:1));
		 $offset = ($page-1) * $perpage;
	      echo "<div class=\"w720 fl\">";
	         echo "<div class=\"common\">";
	             echo "<div class=\"title\">";
					echo "<div class=\"text\"><a href=\"\"><span>".$catname."</span></a></div>";
				 echo "</div>";
				 echo "<div class=\"wrap fix\">";
				 $pageurl = "index.php?f=$module_name&do=categories&catid=$catid";
				 $result_news = $db->sql_query("SELECT id, title, images,hometext,imgtext,source FROM {$prefix}_news WHERE catid='$catid' AND active='1' AND alanguage='$currentlang' ORDER BY time DESC LIMIT $offset, $perpage");
				 if($db->sql_numrows($result_news) > 0) {
				 	$countf_cate = $db->sql_fetchrow($db->sql_query("SELECT COUNT(*) FROM {$prefix}_news WHERE active=1 AND catid='$catid' AND alanguage='$currentlang'"));
					$total_cate = ($countf_cate[0]) ? $countf_cate[0] : 1;
					$get_path = get_path(time());
		            $path_upload_img = "$path_upload/news/$get_path";
					while(list($id, $title, $images,$hometext,$imgtext,$source) = $db->sql_fetchrow($result_news)) 
                    {
                    	$urlid = "$siteurl/".url_sid("index.php?f=$module_name&do=detail&id=$id");
					    echo "<div class=\"news\">";
									echo "<h2 class=\"name\"><a href=\"".$urlid."\" title=\"\">$title</a></h2>";
									echo "<div class=\"article fix\">";
										
										if($images=="")
										{
										     echo "<img width='$sizenews' src=\"images/nopic2.gif\" alt=\"\"/>";
										}else {
											
											 echo "<img width='$sizenews' src=\"$path_upload_img/$images\" alt=\"\"/>";
										}
										echo "</a>";
										echo "<p class=\"short\">";
											echo "$hometext"; 
										echo "</p>";	
										echo "<p>";
											echo "<a href=\"".$urlid."\" class=\"readmore\">"._VIEW_DETAIL."</a>";
										echo "</p>";
									echo "</div>";
						echo "</div>";
                    }
				    if($total_cate > $perpage) {		
						echo "<div class=\"page fix\">";
							echo "".paging_theme($total_cate,$pageurl,$perpage,$page)."";
						echo "</div>";
					  }
				  }else{
				  	echo "<div style=\"text-align:center;padding-top:10px\">"._NEWS_NULL."</div>";
				  }
				 echo "</div>";
	         echo "</div>";
	      echo "</div>";
	 }
	 else
	 {
	 	 /*
	     * Phan lay ra danh sach tin tuc
	     * Le duc vien
	     */
	     $perpage = $news_ccd;
		 $page = intval(isset($_GET['page']) ? $_GET['page'] : (isset($_POST['page']) ? $_POST['page']:1));
		 $offset = ($page-1) * $perpage;
	 	 echo "<div class=\"w720 fl\">";
	         echo "<div class=\"common\">";
	             echo "<div class=\"title\">";
					echo "<div class=\"text\"><a href=\"\"><span>".$catname."</span></a></div>";
				 echo "</div>";
				 echo "<div class=\"wrap fix\">";
				 $pageurl = "index.php?f=$module_name&do=categories&catid=$catid";
				 $result_news = $db->sql_query("SELECT id, title, images,hometext,imgtext,source FROM {$prefix}_news WHERE catid IN($item_list_tmp) AND active='1' AND alanguage='$currentlang' ORDER BY time DESC LIMIT $offset, $perpage");
				 if($db->sql_numrows($result_news) > 0) {
				 	$countf_cate = $db->sql_fetchrow($db->sql_query("SELECT COUNT(*) FROM {$prefix}_news WHERE active=1 AND catid IN($item_list_tmp) AND alanguage='$currentlang'"));
					$total_cate = ($countf_cate[0]) ? $countf_cate[0] : 1;
					$get_path = get_path(time());
		            $path_upload_img = "$path_upload/news/$get_path";
					while(list($id, $title, $images,$hometext,$imgtext,$source) = $db->sql_fetchrow($result_news)) 
                    {
                    	$urlid = "$siteurl/".url_sid("index.php?f=$module_name&do=detail&id=$id");
					    echo "<div class=\"news\">";
									echo "<h2 class=\"name\"><a href=\"".$urlid."\" title=\"\">$title</a></h2>";
									echo "<div class=\"article fix\">";
									
										if($images=="")
										{
										     echo "<img width='$sizenews' src=\"images/nopic2.gif\" alt=\"\"/>";
										}else {
											
											 echo "<img width='$sizenews' src=\"$path_upload_img/$images\" alt=\"\"/>";
										}
										echo "</a>";
										echo "<p class=\"short\">";
											echo "$hometext"; 
										echo "</p>";	
										echo "<p>";
											echo "<a href=\"".$urlid."\" class=\"readmore\">"._VIEW_DETAIL."</a>";
										echo "</p>";
									echo "</div>";
						echo "</div>";
                    }
				    if($total_cate > $perpage) {		
						echo "<div class=\"page fix\">";
							echo "".paging_theme($total_cate,$pageurl,$perpage,$page)."";
						echo "</div>";
					  }
				  }else{
				  	echo "<div style=\"text-align:center;padding-top:10px\">"._NEWS_NULL."</div>";
				  }
				 echo "</div>";
	         echo "</div>";
	      echo "</div>";
	 }
	     /*
	      * Ket thuc phan tin tuc
	      */
	     include_once("templates/".$Default_Temp."/rightbody.php");
	 echo "</div>";
	 themeSlideShow(8);
	 themeFooter();
     echo "</div>";
     
include_once("templates/".$Default_Temp."/footer.php");
}
?>